package com.hujh.android;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ToastActivity extends Activity {

	Button btn;  
    
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState); 
        
        setContentView(R.layout.activity_toast);  
        
        ViewGroup group = (ViewGroup)this.findViewById(R.id.image_toast);
        
        
        ImageView imageView = new ImageView(ToastActivity.this);    
        imageView.setLayoutParams(new LayoutParams(20,20));    
        imageView.setPadding(20, 0, 20, 0); 
        imageView.setBackgroundColor(Color.BLUE);
        group.addView(imageView);
        
        
        btn = (Button)findViewById(R.id.show);  
        btn.setOnClickListener(new View.OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                //标准方式  
                showToast1();  
                //标准方式上添加图片  
                showToast2();  
                //自定义显示方式  
                showToast3();  
            }  
        });  
    }  
      
    /**  
     * Basic Standard Toast  
     * 标准提示信息方式  
     */  
    private void showToast1(){  
       Toast toast = Toast.makeText(getApplicationContext(),"Hello, This is hujh!", Toast.LENGTH_LONG);  
       toast.show();  
    }  
      
    /**  
     * Adding an Image to the Standard Toast  
     * 在标准显示方式基础上添加图片  
     */  
    private void showToast2(){  
       Toast toast = Toast.makeText(getApplicationContext(),"Hello, This is hujh!", Toast.LENGTH_LONG);  
        toast.setGravity(Gravity.CENTER, 0, 0);  
        LinearLayout toastView = (LinearLayout) toast.getView();  
        ImageView imageCodeProject = new ImageView(getApplicationContext());  
        imageCodeProject.setImageResource(R.drawable.ic_launcher);  
        toastView.addView(imageCodeProject, 0);  
        toast.show();  
    }  
      
    /**  
     * Creating a Toast with Custom Layout  
     * 创建自定义的提示信息方式  
     */  
    private void showToast3(){  
        LayoutInflater inflater = getLayoutInflater();  
        View layout = inflater.inflate(R.layout.toast,  
           (ViewGroup) findViewById(R.id.llToast));  
        ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);  
        image.setImageResource(R.drawable.ic_launcher);  
        TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);  
        title.setText("Attention");  
        TextView text = (TextView) layout.findViewById(R.id.tvTextToast);  
        text.setText("Hello, This is hujh!");  
        Toast toast = new Toast(getApplicationContext());  
        toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);  
        toast.setDuration(Toast.LENGTH_LONG);  
        toast.setView(layout);  
        toast.show();  
    }  
}
